// 如何将两个端点变成一个端点？
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 55;
typedef pair<int, int> PII;
int n, m, ans = 0;
char cow[N][N];
bool flag[N][N];
queue<PII> q;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void print()
{
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
            cout << cow[i][j] << ' ';
        puts("");
    }
    cout << endl;
}

bool out(int x, int y)
{
    for (int i = 0; i < 4; ++i)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (cow[xx][yy] == '.')
            return true;
    }
    return false;
}

void dfs(int i, int j)
{
    flag[i][j] = true;
    // cout << i << ' ' << j << endl;
    if (i == 1 || i == n || j == 1 || j == m || out(i, j))
    {
        // cout << i << ' ' << j << endl;
        // cow[i][j] = '+';
        q.push({i, j});
    }

    for (int u = 0; u < 4; ++u)
    {
        int x = i + dx[u], y = j + dy[u];
        if (x > 0 && x <= n && y > 0 && y <= m && !flag[x][y] && cow[x][y] == 'X')
            dfs(x, y);
    }
}

void bfs()
{
    while (!q.empty())
    {
        int sz = q.size();
        // cout << sz << endl;
        ++ans;
        while (sz--)
        {
            const auto &top = q.front();
            q.pop();
            int x = top.first, y = top.second;
            for (int i = 0; i < 4; ++i)
            {
                int xx = x + dx[i], yy = y + dy[i];
                if (xx > 0 && xx <= n && yy > 0 && yy <= m && !flag[xx][yy])
                {
                    if (cow[xx][yy] == '.')
                    {
                        cow[xx][yy] = '+';
                        q.push({xx, yy});
                    }
                    else if (cow[xx][yy] == 'X')
                        return;
                }
            }
        }
        // print();
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        scanf("%s", cow[i] + 1);
    bool is_first = true;
    // 找到一个斑点的所有边界点
    for (int i = 1; i <= n && is_first; ++i)
        for (int j = 1; j <= m; ++j)
            if (cow[i][j] == 'X' && !flag[i][j])
            {
                dfs(i, j);
                is_first = false;
                break;
            }
    // print();
    bfs();
    cout << ans - 1 << endl;
    // print();
    return 0;
}